﻿using System;
using System.Collections.Generic;
using MediaMover.Common.TV;
using MediaMover.Data.NHIbernate;
using MediaMover.DataAccess;
using NHibernate.Criterion;

namespace MediaMover.Data.Repositories
{
    public class ShowRepository : IShowRepository
    {
        public void Add(Show show)
        {
            using (var session = Helper.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    session.Save(show);
                    transaction.Commit();
                }
            }
        }

        public void Update(Show show)
        {
            using (var session = Helper.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    session.Update(show);
                    transaction.Commit();
                }
            }
        }

        public void Remove(Show show)
        {
            using (var session = Helper.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    session.Delete(show);
                    transaction.Commit();
                }
            }
        }

        public Show GetById(int showId)
        {
            using (var session = Helper.OpenSession())
            {
                return session.Get<Show>(showId);
            }
        }

        public Show GetByTitle(string title)
        {
            using (var session = Helper.OpenSession())
            {
                var show = session.CreateCriteria(typeof (Show)).Add(Restrictions.Eq("Title", title)).UniqueResult<Show>();
                return show;
            }
        }

        public IEnumerable<Show> GetShows()
        {
            using (var session = Helper.OpenSession())
            {
                var shows = session.CreateCriteria(typeof (Show)).List<Show>();
                return shows;
            }
        }
    }
}
